﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Kotonoha.Objects;
using Kotonoha.Server.Objects.Model;
using Kotonoha.Server.Utils;
using System.Linq;

namespace Kotonoha.Server.UI {
    public partial class KotonohaMain {
        private ShowRepeatedWordProxy wordProxy;

        private void Repeat_GridLoaded(object sender, RoutedEventArgs e) {
            Grid grid = (Grid) sender;
            wordProxy = new ShowRepeatedWordProxy(this.words);
            grid.DataContext = wordProxy;

            grid.PreviewKeyDown += GridOnPreviewKeyDown;
        }

        private void GridOnPreviewKeyDown(object sender, KeyEventArgs args) {
            args.Handled = true;
        }

        private void ShowButton_Click(object sender, RoutedEventArgs e) {
            wordProxy.ShowAll();
        }

        private void ScoreButton_Click(object sender, RoutedEventArgs e) {
            Button btn = sender as Button;
            if (btn != null) {
                wordProxy.ProcessScore(int.Parse((string) btn.Tag));
            }
        }

        private void RepeatTab_GotFocus(object sender, RoutedEventArgs e) {
            wordProxy.SelectNextWord();
        }
        
    }

    public class ShowRepeatedWordProxy : INotifyPropertyChanged {
        private readonly WordHolder words;
        public event PropertyChangedEventHandler PropertyChanged;

        private Word word;

        public bool scoreVisible;

        public ShowRepeatedWordProxy(WordHolder words) {
            this.words = words;
        }

        public void FirePropertyChanged(params string[] propertyNames) {
            if (PropertyChanged != null) {
                foreach (var propertyName in propertyNames) {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }

        public void ProcessScore(int i) {
            throw new NotImplementedException();
        }

        public void ShowAll() {
            throw new NotImplementedException();
        }

        public void SelectNextWord() {
            word = FindWordCore();
            if (word == null) {
                //TODO:lock form, make only next btn available
            }
            FirePropertyChanged("Writing", "Reading", "Examples", "Meaning", "ScoreBtnVisible", "NextBtnVisible");
        }

        private Word FindWordCore() {
            Word found = words.Collection
                .Where(w => w.LearningData != null)
                .Where(w => w.LearningData.IntervalEnd > DateTime.Now)
                .FirstOrDefault();

            if (found != null) {
                return found;
            }
            found = words.Collection.FirstOrDefault(w => w.LearningData == null);
            return found;
        }


        private Visibility Vis(bool val) {
            return val ? Visibility.Visible : Visibility.Hidden;
        }

#region Properties
        public Visibility ScoreBtnVisible {
            get { return Vis(scoreVisible); }
        }

        public Visibility NextBtnVisible {
            get { return Vis(!scoreVisible); }
        }

        public string Writing {
            get { return word.Writing; }
        }

        public string Reading {
            get { return word.Reading; }
        }

        public string Meaning {
            get { return StringUtilites.Combine(word.Translations, "\n"); }
        }

        public string Examples {
            get { return StringUtilites.Combine(
                word.Examples.Select(ex => string.Format("{0}\n{1}", ex.Content, ex.Translation)),
                    "\n"); }
        }
#endregion
    }
}